package com.zwb.demo.easyweb.common.entity;

import com.zwb.demo.easyweb.common.entity.converter.DataStateEnumConverter;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.util.Date;

/**
 * 实体类的基类 MappedSuperclass会把父类的字段映射到子类上面
 *
 * @author zwb
 */
@MappedSuperclass
@EqualsAndHashCode(callSuper = false)
@Data
public class BaseEntity {

    @Column(
            name = "create_time",
            nullable = false,
            updatable = false,
            columnDefinition = "datetime")
    @ApiModelProperty(value = "创建时间")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    @Column(name = "update_time", columnDefinition = "datetime")
    @ApiModelProperty(value = "修改时间")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    @ApiModelProperty(value = "数据状态")
    @Convert(converter = DataStateEnumConverter.class)
    @Column(name = "date_status", nullable = false, columnDefinition = "int")
    private DataStateEnum dataState;

    @PrePersist
    protected void prePersist() {
        createTime = new Date();
        if (dataState == null) {
            dataState = DataStateEnum.NORMAL;
        }
    }

    @PreUpdate
    protected void preUpdate() {
        updateTime = new Date();
    }
}
